/*
Jump Game

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Determine if you are able to reach the last index.

For example:
A = [2,3,1,1,4], return true.

A = [3,2,1,0,4], return false.

*/
#include <iostream>
#include <vector>
#include <map>

using namespace std;

class Solution1 {
public:
	bool canJump(vector<int>& nums) {
		int size = nums.size();
		if (size == 1 && (nums[size -1] == 0 || nums[size -1] == 1))
		{
			return true;
		}
		if (size <= 1)
			return false;
		map<int,int> dict;

		for (int i = 0; i < size; i++)
		{
			dict.insert(pair<int, int>(i, nums[i]));
		}
		
		int start = nums[0], end = nums[size - 1];
		int step;
		int nextPoint, curPoint = start;
		if (curPoint > end)return false;

		while (curPoint < end)
		{
			step = dict[curPoint];
			nextPoint = curPoint + step;
			if (nextPoint == curPoint)break;
			else
				curPoint = nextPoint;
		}

		if (curPoint >= end)
			return true;
		else
		{
			return false;
		}
		
	}
};


class Solution {
public:
	bool canJump(vector<int>& nums) {
		
		int size = nums.size();
		if (size == 0 || size == 1)
		{
			return true;
		}

		int maxStep = nums[0];
		for (int i = 0; i < size; i++)
		{
			if (maxStep == 0)return false;
			maxStep--;
			if (maxStep<nums[i])
			{
				maxStep = nums[i];
			}
			if (maxStep + i > size -1)
			{
				return true;
			}

		}


	}
};

int main(int argc, char * argv[])
{

	vector<int> nums;
	int a;


	for (int i = 1; i < argc; i++)
	{
		cout <<argv[i] << " ";
		a = atoi(argv[i]);
		nums.push_back(a);
	}

	cout << endl;
	cout << "The size of nums: " <<nums.size()<< endl;

	Solution s;
	if (s.canJump(nums))
	{
		cout <<"true"<< endl;
	}
	else{
		cout << "false" << endl;

	
	}


	system("pause");
	return 0;
}